package com.hyt.it.ogt.controller.ykcj.exam;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.annotation.Log;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.BizErrorCode;
import com.hyt.it.ogt.ykcj.common.enums.BusinessType;
import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.common.utils.SecurityUtils;
import com.hyt.it.ogt.ykcj.common.utils.poi.ExcelUtil;
import com.hyt.it.ogt.ykcj.domain.exam.ExamNameConfig;
import com.hyt.it.ogt.ykcj.domain.vo.ExamConfigNameVo;
import com.hyt.it.ogt.ykcj.service.exam.IExamInfoService;
import com.hyt.it.ogt.ykcj.service.exam.IExamNameConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;

/**
 * 考试名称配置Controller
 *
 * @author admin
 * @date 2021-06-18
 */
@Api(tags = "exam.2.考试名称配置", value = "考试名称请求接口")
@ApiSort(value = 102)
@RestController
@RequestMapping("/exam/config")
public class ExamNameConfigController extends BaseController
{
    @Autowired
    private IExamNameConfigService examNameConfigService;

    @Autowired
    private IExamInfoService iExamInfoService;

    /**
     * 查询考试名称配置列表
     */
    @ApiOperation("查询考试名称配置列表")
    @GetMapping("/list")
    public TableDataInfo list(ExamNameConfig examNameConfig)
    {
        startPage();
        List<ExamNameConfig> list = examNameConfigService.selectExamNameConfigList(examNameConfig);
        return getDataTable(list);
    }

    /**
     * 导出考试名称配置列表
     */
    @ApiOperation("导出考试名称配置列表")
    @Log(title = "考试名称配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ExamNameConfig examNameConfig)
    {
        List<ExamNameConfig> list = examNameConfigService.selectExamNameConfigList(examNameConfig);
        ExcelUtil<ExamNameConfig> util = new ExcelUtil<ExamNameConfig>(ExamNameConfig.class);
        return util.exportExcel(list, "考试名称配置数据");
    }

    /**
     * 获取考试名称配置详细信息
     * @throws Exception
     */
    @ApiOperation("获取考试名称配置详细信息")
//    @RequestMapping(value = "/{id}/{year}", method = RequestMethod.GET)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@RequestParam("id") String id, @RequestParam("year") String year) throws Exception
    {
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error(BizErrorCode.EXAM_E0002.getErrMsg());
        }
        return AjaxResult.success(iExamInfoService.selectExamInfoByExamNameId(id,year));
    }

    /**
     * 新增考试名称配置
     */
    @ApiOperation("新增考试名称配置")
    @Log(title = "考试名称配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamNameConfig examNameConfig)
    {
        if(StringUtils.isEmpty(examNameConfig.getName())){
            return AjaxResult.error("请填写考试名称");
        }
        if(examNameConfigService.checkExamNameIsExist(examNameConfig)>0){
            return AjaxResult.error("考试名称已存在");
        }
        if(StringUtils.isEmpty(examNameConfig.getExamType())){
            return AjaxResult.error("请选择考试类型");
        }
        examNameConfig.setCreateBy(SecurityUtils.getUsername());
        examNameConfig.setCreateTime(DateUtils.getNowDate());
        examNameConfig.setUpdateBy(SecurityUtils.getUsername());
        examNameConfig.setUpdateTime(DateUtils.getNowDate());
        return toAjax(examNameConfigService.insertExamNameConfig(examNameConfig));
    }

    /**
     * 修改考试名称配置
     */
    @ApiOperation("修改考试名称配置")
    @Log(title = "考试名称配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamNameConfig examNameConfig)
    {
        if(StringUtils.isEmpty(examNameConfig.getId())){
            return AjaxResult.error("请填写考试名称");
        }
        if(StringUtils.isEmpty(examNameConfig.getName())){
            return AjaxResult.error("请填写考试名称");
        }
        if(StringUtils.isEmpty(examNameConfig.getExamType())){
            return AjaxResult.error("请选择考试类型");
        }
        examNameConfig.setUpdateBy(SecurityUtils.getUsername());
        examNameConfig.setUpdateTime(new Date());
        return toAjax(examNameConfigService.updateExamNameConfig(examNameConfig));
    }

    /**
     * 删除考试名称配置
     * @throws Exception
     */
    @ApiOperation("删除考试名称配置")
    @Log(title = "考试名称配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String [] ids) throws Exception
    {
        if(ids== null || ids.length == 0){
            return AjaxResult.error("参数不能为空");
        }
        return toAjax(examNameConfigService.deleteExamNameConfigByIds(ids));
    }

    /**
     * 根据考试类型查询全部名称
     * @param examType （0:专业资格考试 1:公务员考试）
     * @return （0:考试 1:科目）
     */
    @ApiOperation("根据考试类型查询全部名称")
    @ApiImplicitParam(name = "examType", value = "考试类型（0:专业资格考试 1:公务员考试）", dataType = "String")
    @GetMapping("/findAllConfig/{examType}")
    public TableDataInfo findAllConfig(@PathVariable("examType") String examType)
    {
        List<ExamConfigNameVo> list = examNameConfigService.findAllExamConfig(examType);
        return getDataTable(list);
    }


    /**
     * 查询科目名称配置列表
     */
    @ApiOperation("查询科目名称配置列表")
    @GetMapping("/getExamConfigList")
    public AjaxResult getExamConfigList()
    {
        List<ExamConfigNameVo> list = examNameConfigService.getExamConfigList();
        return AjaxResult.success(list);
    }
}



